<?php require_once('Connections/db_conn.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<?php

$workshop_id = $_GET['id'];
mysql_select_db($database_db_conn, $db_conn);

$query_Language = "SELECT lang.LANGID LANGID, lang.LANG LANG FROM `language` lang	ORDER BY lang.LANG";

$Language = mysql_query($query_Language, $db_conn) or die(mysql_error());


$query_Ethnicity = "SELECT etn.ETHNICITYID ETHNICITYID , etn.ETHNICITY ETHNICITY FROM `ETHNICITY` etn
						ORDER BY etn.ETHNICITYID";

$Ethnicity = mysql_query($query_Ethnicity, $db_conn) or die(mysql_error());

$query_PartType = "SELECT tp.TYPEID TYPEID, tp.TYPE TYPE FROM `TYPE` tp ORDER BY tp.TYPEID";
$type = mysql_query($query_PartType, $db_conn) or die(mysql_error());

$layout = explode('&&&', file_get_contents('layout.html'));
echo $layout[0];

session_start();
if (!(isset($_SESSION['username'])))
	header("location:login.php");
?>

<html>
<body>

<!-- Begin Main Column -->

<div id="mainContent">

	<h2>Step 3: Participant Information</h2>
	<p>Search for Participant</p>
  <form id="pform" method="post" action="">
          <table width="200" border="0">
            <tr>
              <td>First Name:</td>
              <td><label>
                <input type="text" name="first_name_participant" id="first_name_participant" />
              	</label></td>
            </tr>
            <tr>
              <td>Last Name:</td>
              <td><label>
                <input type="text" name="last_name_participant" id="last_name_participant" />
              	</label></td>
            </tr>
            <tr>
              <td>Address:</td>
              <td><label>
                <input type="text" name="address_participant" id="address_participant" />
              	</label></td>
              <td width="100"><input type="submit" name="participant_search" id="participant_search" value="Search" class="button"/>
           		</label></td>
            </tr>
          </table>
  </form>
  <?php
		//Check if pform was submitted
	 	if($_POST[participant_search]=='Search')
	 	{
			//Get Data from form and check for MySQL injections
			$first_name_participant=$_POST['first_name_participant'];
			$last_name_participant=$_POST['last_name_participant'];
			$address_participant=$_POST['address_participant'];
			$first_name_participant = stripslashes($first_name_participant);
			$last_name_participant = stripslashes($last_name_participant);
			$address_participant = stripslashes($address_participant);
			$first_name_participant = mysql_real_escape_string($first_name_participant);
			$last_name_participant = mysql_real_escape_string($last_name_participant);
			$address_participant = mysql_real_escape_string($address_participant);
			
			//Display participants matching search criteria in a selectable table
			$Rest="";
		  	if($first_name_participant!="")
		  	{
				if($Rest=="")
					$Rest.="where FIRSTNAME LIKE '%$first_name_participant%' ";

				else
					$Rest.="AND FIRSTNAME LIKE '%$first_name_participant%' ";
			}
			if($last_name_participant!="")
			{
				if($Rest=="")
					$Rest.="where LASTNAME LIKE '%$last_name_participant%' ";
				else
					$Rest.="AND LASTNAME LIKE '%$last_name_participant%' ";
		  	}
		  	if($address_participant!="")
		  	{
				if($Rest=="")
					$Rest.="where ADDRESS LIKE '%$address_participant%' ";
				else
					$Rest.="AND ADDRESS LIKE '%$address_participant%' ";
			}
			$participant_by_name = mysql_query("
				SELECT PARTICIPANTID, FIRSTNAME, LASTNAME, CITY, ADDRESS FROM PARTICIPANTS $Rest ORDER BY LASTNAME, FIRSTNAME");
	?>
            <form name="selectform" method="post" action="">
            <table border="1" >
            <tr>
                <th align="center">#</th>
                <th align="center">First</th>
                <th align="center">Last</th>
                <th align="center">City</th>
                <th align="center">Address</th>
            </tr>
            <?php
            while($rows=mysql_fetch_array($participant_by_name)){
            ?>
            <tr>
                <td><input name="pid" type="radio" id="pid" value="<?php echo $rows['PARTICIPANTID']; ?>"></td>
                <td><?php echo $rows['FIRSTNAME']; ?></td>
                <td><?php echo $rows['LASTNAME']; ?></td>
                <td><?php echo $rows['CITY']; ?></td>
                <td><?php echo $rows['ADDRESS']; ?></td>
            </tr>
            <?php
            }
            ?>
            </table>
            <p>
	    	<label>
           		<input name="selectpid" type="submit" id="selectpid" value="Add Participant" class="button" />
            </label>
            </p>
            </form>
	<?php
		}

		//Check if selectform was submitted
		else if($_POST[selectpid]=='Add Participant' )
		{
			$pid=$_POST["pid"];
			$error=0;

			//Check to make sure required fields are inputted
			if($pid!="")
			{
				echo "<META HTTP-EQUIV='refresh' content='0;URL=EditParticipant.php?id=$pid&wid=$workshop_id'>";



			}
			else
			{
 			echo "Required fields needed.";
				$error=1;
			}

		}

		//Check if form1 was submitted
		else if($_POST[submitp]=='Add Participant')
		{
			//Get Data from form and check for MySQL injections
			$first_name=$_POST['first_name'];
			$last_name=$_POST['last_name'];
			$address=$_POST['address'];
			$city=$_POST['city'];
			$zipcode=$_POST['zipcode'];
			$phone_number=$_POST['phone_number'];
			$email=$_POST['email'];
			$participant_lang = $_POST["part_lang"];
			$language_other = $_POST['language_other'];
			$ethnicity_other = $_POST['ethnicity_other'];
			$type_other = $_POST['type_other'];
			$gender=$_POST['gender'];
			$youngerthan20=$_POST['youngerthan20'];
			$participant_ethnicity = $_POST["ethnicity"];
			$participant_type = $_POST["part_type"];

			$number_of_children=$_POST['number_of_children'];
			$months_0_12=$_POST['months_0_12'];
			$months_13_23=$_POST['months_13_23'];
			$years_2_4=$_POST['years_2_4'];
			$years_5_6=$_POST['years_5_6'];
			$years_7_8=$_POST['years_7_8'];
			$over_8=$_POST['over_8'];
			$disabilities=$_POST['disabilities'];
			$member_info=$_POST['member_info'];

			$first_name = stripslashes($first_name);
			$last_name = stripslashes($last_name);
			$address = stripslashes($address);
			$city = stripslashes($city);
			$zipcode = stripslashes($zipcode);
			$phone_number = stripslashes($phone_number);
			$email = stripslashes($email);
			$number_of_children = stripslashes($number_of_children);
			$language_other = stripslashes($language_other);
			$ethnicity_other = stripslashes($ethnicity_other);
			$type_other = stripslashes($type_other);

			$first_name = mysql_real_escape_string($first_name);
			$last_name = mysql_real_escape_string($last_name);
			$address = mysql_real_escape_string($address);
			$city = mysql_real_escape_string($city);
			$zipcode = mysql_real_escape_string($zipcode);
			$phone_number = mysql_real_escape_string($phone_number);
			$email = mysql_real_escape_string($email);
			$number_of_children = mysql_real_escape_string($number_of_children);
			$ethnicity_other = mysql_real_escape_string($ethnicity_other);
			$type_other = mysql_real_escape_string($type_other);
			$language_other = mysql_real_escape_string($language_other);
			
			$error=0;
			//Check to make sure required fields are inputted
			if($first_name!='' && $last_name!='')
			{
				mysql_query("INSERT INTO
					PARTICIPANTS (FIRSTNAME, LASTNAME, ADDRESS, CITY, ZIPCODE, PHONENO, EMAIL, GENDER, YOUNGERTHAN20,
					HASDISABILITIES, SENDNEWSLETTER, SENDMEMBERINFO, NUMBEROFCHILDREN, MONTHS_0_12, MONTHS_13_23, YEARS_2_4,
					YEARS_5_6,YEARS_7_8, YEARS_OVER_8) VALUES ('$first_name', '$last_name', '$address', '$city', '$zipcode',
					'$phone_number', '$email', '$gender', '$youngerthan20', '$disabilities', '$newsletter', '$member_info',
					'$number_of_children', '$months_0_12', '$months_13_23', '$years_2_4', '$years_5_6', '$years_7_8',
					'$years_over_8')");

				$get_participant_id=mysql_query("select@@IDENTITY");
				$participant_id=mysql_result($get_participant_id,0);

				mysql_query("INSERT INTO
					WORKSHOPPARTICIPANTS (WRKSPID, PARTICIPANTID, NUMBEROFCHILDREN)
					VALUES ('$workshop_id', '$participant_id', '$number_of_children')");

				if (!empty($participant_lang))
				{
					foreach ($participant_lang as $k => $v) {
						 $sql_lang = "INSERT INTO PARTICIPANTLANGUAGE (LANGID , PARTICIPANTID ) VALUES ($v,$participant_id)";
						if (mysql_query($sql_lang))
							 echo "Language has been updated";
						else
						{
							echo("<P>Error adding Language: $sql_lang " . mysql_error() . "</P>");
						}

					}
					$query_Language = "SELECT LANG FROM language";
					$Language = mysql_query($query_Language, $db_conn) or die(mysql_error());
					if($language_other != NULL)
					{
						while ($row_Language = mysql_fetch_assoc($Language)){
							if($row_Language['LANG'] == $language_other)
							{
								$lang_yes = 1;
								break;
							}
							else
							{
								$lang_yes = 0;
							}
						}
						if($lang_yes == 0)
						{
							mysql_query("INSERT INTO language (LANG) VALUES ('$language_other')");
							$get_lang_id=mysql_query("select@@IDENTITY");
							$lang_id=mysql_result($get_lang_id,0);
							mysql_query("INSERT INTO PARTICIPANTLANGUAGE (LANGID , PARTICIPANTID ) 
								VALUES ($lang_id,$participant_id)");
						}
					}
				}
				if (!empty($participant_ethnicity))
				{
					foreach ($participant_ethnicity as $k => $v) {
						$sql_eth = "INSERT INTO PARTICIPANTETHNICITY(ETHNICITYID, PARTICIPANTID ) VALUES ($v,$participant_id)";
						if (mysql_query($sql_eth))
							echo "Ethnicity has been updated";
						else
						{
							echo("<P>Error adding ethnicity: $sql_eth" . mysql_error() . "</P>");
						}

					}
					$query_Ethnicity = "SELECT ETHNICITY FROM ethnicity";
					$Ethnicity = mysql_query($query_Ethnicity, $db_conn) or die(mysql_error());
					if($ethnicity_other != NULL)
					{
						while ($row_Ethnicity = mysql_fetch_assoc($Ethnicity)){
							if($row_Ethnicity['ETHNICITY'] == $ethnicity_other)
							{
								$eth_yes = 1;
								break;
							}
							else
							{
								$eth_yes = 0;
							}
						}
						if($eth_yes == 0)
						{
							mysql_query("INSERT INTO ethnicity (ETHNICITY) VALUES ('$ethnicity_other')");
							$get_eth_id=mysql_query("select@@IDENTITY");
							$eth_id=mysql_result($get_eth_id,0);
							mysql_query("INSERT INTO PARTICIPANTETHNICITY(ETHNICITYID , PARTICIPANTID ) 
								VALUES ($eth_id,$participant_id)");
						}
					}
				}
				if (!empty($participant_type))
				{
					foreach ($participant_type as $k => $v) {
						$sql_type = "INSERT INTO PARTICIPANTTYPE(TYPEID , PARTICIPANTID ) VALUES ($v,$participant_id)";
						if (mysql_query($sql_type))
							echo "Type has been updated";
						else
						{
							echo("<P>Error adding type: $sql_eth" . mysql_error() . "</P>");
						}
					}
					$query_Type = "SELECT TYPE FROM type";
					$Type = mysql_query($query_Type, $db_conn) or die(mysql_error());
					if($type_other != NULL)
					{
						while ($row_Type = mysql_fetch_assoc($Type)){
							if($row_Type['TYPE'] == $type_other)
							{
								$type_yes = 1;
								break;
							}
							else
							{
								$type_yes = 0;
							}
						}
						if($type_yes == 0)
						{
							mysql_query("INSERT INTO type (TYPE) VALUES ('$type_other')");
							$get_type_id=mysql_query("select@@IDENTITY");
							$type_id=mysql_result($get_type_id,0);
							mysql_query("INSERT INTO PARTICIPANTTYPE(TYPEID , PARTICIPANTID ) VALUES ($type_id,$participant_id)");
						}
					}
				}
			}
			else
			{
				echo "Required fields needed.";
				$error=1;
			}

			if(!$error)
			{
				//Redirect
					echo "<META HTTP-EQUIV='refresh' content='0;URL=EditEvent.php?id=$workshop_id'>";
			}
		}

		ob_end_flush();


	?>
	<p>--OR--</p>
<p>Add a Participant</p>
    Note: Required fields denoted with an asterisk(*)
  <form id="form1" method="post" action="">
    <table width="402" border="0">
      <tr>
        <td width="121">* First Name:</td>
        <td width="271"><label>
            <input type="text" name="first_name" id="first_name" />
        </label></td>
      </tr>
      <tr>
        <td>* Last Name:</td>
        <td><label>
            <input type="text" name="last_name" id="last_name" />
        </label></td>
      </tr>
      <tr>
        <td>Address:</td>
        <td><label>
            <input type="text" name="address" id="address" />
        </label></td>
      </tr>
      <tr>
        <td>City:</td>
        <td><label>
            <input type="text" name="city" id="city" />
        </label></td>
      </tr>
      <tr>
        <td>Zip Code:</td>
        <td><label>
            <input name="zipcode" type="text" id="zipcode" size="5" maxlength="5" />
        </label></td>
      </tr>
      <tr>
        <td>Phone Number:</td>
        <td><label>
            <input name="phone_number" type="text" id="phone_number" size="10" maxlength="10" />
        </label></td>
      </tr>
      <tr>
        <td>Email:</td>
        <td><label>
            <input type="text" name="email" id="email" />
        </label></td>
      </tr>
      <tr>
        <td height="33">Participant Type:</td>
        <td>
			<?php
				 while ($row_type = mysql_fetch_assoc($type))
				 {
				 	echo "<input type='checkbox' name='part_type[]' value='".$row_type['TYPEID']."' />   ".$row_type['TYPE']."<br />";
				  }
				 mysql_free_result($type);
	  		?>
	  		 Other:
        <input type="text" name="type_other" id="type_other" />
        </td>
      </tr>
      <tr>
        <td height="36">Gender:</td>
        <td>
            <input type="radio" name="gender" value="Male" id="gender">
            Male
            <input type="radio" name="gender" value="Female">
            Female
        </td>
      </tr>
      <tr>
        <td>Younger than 20:</td>
        <td>
            <input type="radio" name="youngerthan20" value="Yes" id="youngerthan20">
            Yes
            <input type="radio" name="youngerthan20" value="No">
            No
		</td>
      </tr>
      <tr>
        <td height="28">Ethnicity:</td>
        <td>
			<?php
				while ($row_ethc = mysql_fetch_assoc($Ethnicity))
				{
					echo "<input type='checkbox' name='ethnicity[]' value='".$row_ethc['ETHNICITYID']."' />   ".$row_ethc['ETHNICITY']."<br />";
				}
				mysql_free_result($Ethnicity);

	  		?>
	  		 Other:
        <input type="text" name="ethnicity_other" id="ethnicity_other" />
        </td>
      </tr>
      <tr>
        <td>Number of Children:</td>
        <td><label>
            <input type="text" name="number_of_children" id="number_of_children" />
        </label></td>
      </tr>
      <tr>
        <td>Children's Age:</td>
        <td>
            <input type="checkbox" name="months_0_12" id="months_0_12" />
        0-12 Months<br />
          <input type="checkbox" name="months_13_23" id="months_13_23" />
        13-23 Months<br />
          <input type="checkbox" name="years_2_4" id="years_2_4" />
        2-4 Years<br />
          <input type="checkbox" name="years_5_6" id="years_5_6" />
        5-6 Years<br />
          <input type="checkbox" name="years_7_8" id="years_7_8" />
        7-8 Years<br />
          <input type="checkbox" name="over_8" id="over_8" />
        Over 8 Years</td>
      </tr>
      <tr>
        <td>Has Disabilities:</td>
        <td>
            <input type="radio" name="disabilities" value="Yes" id="disabilities" />
            Yes
            <input type="radio" name="disabilities" value="No" />
            No
        </p></td>
      </tr>
      <tr>
        <td>Languages:</td>
        <td>
		  <?php
			  while ($row_Language = mysql_fetch_assoc($Language))
			  {
			  	echo "<input type='checkbox' name='part_lang[]' value='".$row_Language['LANGID']."' />   ".$row_Language['LANG']."<br />";
			  }
			  mysql_free_result($Language);
		  ?>
		   Other:
        <input type="text" name="language_other" id="language_other" />
        </td>
      </tr>
      <tr>
        <td>Send Newsletter:</td>
        <td>
            <input type="radio" name="newsletter" value="Yes" id="newsletter" />
            Yes
            <input type="radio" name="newsletter" value="No"/>
            No
        </td>
      </tr>
      <tr>
        <td>Send Member Information:</td>
        <td>
            <input type="radio" name="member_info" value="Yes" id="member_info_yes" />
            Yes
            <input type="radio" name="member_info" value="No" id="member_info_no" />
            No
        </td>
      </tr>
      </table>
      <p>
	    <label>
        <input type="submit" name="submitp" id="submitp" value="Add Participant" class="button" />
        </label>
	  </p>
  </form>

</div>

<?php
echo $layout[1];
?>

</body>
</html>